#include <ttg/libs.h>
#include <tbb/task_scheduler_init.h>
#include <tbb/tick_count.h>

#include "serialConvexHull.h"
#include "ttgConvexHull.h"
#include "tbbConvexHull.h"
#include "ResultComparison.h"

int main()
{
	//Initing
	ttg::services::ServiceBase::getRef()->start();
	tbb::task_scheduler_init init(tbb::task_scheduler_init::automatic);
	//Preparing data
	Points pts;
	pts.resize(10000000);
	printf("Generating %u random points... ", pts.size());
	for (size_t i = 0; i < pts.size(); i++) {
		pts[i].x = ttg::math::getFloatRandom(-1.0, 1.0);
		pts[i].y = ttg::math::getFloatRandom(-1.0, 1.0);
//		printf("(%g,%g) ", pts[i].x, pts[i].y);
	}
	printf("Done\n");
	//Serial algorithm.
	printf("Calculating using serial version... ");
	Points serialRes;
	double serialTime = serialConvexHull(pts, serialRes);
	printf("Done\n Elapsed %lf seconds\n", serialTime);
/*	for (int i = 0; i < serialRes.size(); i++)
		printf("(%g,%g) ", serialRes[i].x, serialRes[i].y);
	printf("\n");*/
	//Parallel algorithm (via ttg).
	printf("Calculating using parallel version (via ttg)... ");
	Points ttgRes;
	double ttgTime = ttgConvexHull(pts, ttgRes);
	printf("Done\n Elapsed %lf seconds\n Checking result ... ", ttgTime);
	if (!compareResults(ttgRes, serialRes))
		printf("failed\n");
	else
		printf("ok\n");
/*	for (int i = 0; i < ttgRes.size(); i++)
		printf("(%g,%g) ", ttgRes[i].x, ttgRes[i].y);
	printf("\n");*/
	ttgRes.clear();
	//Parallel algorithm (via tbb).
	printf("Calculating using parallel version (via tbb)... ");
	Points tbbRes;
	double tbbTime = tbbConvexHull(pts, tbbRes);
	printf("Done\n Elapsed %lf seconds\n Checking result ... ", tbbTime);
	if (!compareResults(tbbRes, serialRes))
		printf("failed\n");
	else
		printf("ok\n");
/*	for (int i = 0; i < tbbRes.size(); i++)
		printf("(%g,%g) ", tbbRes[i].x, tbbRes[i].y);
	printf("\n");*/
	return 0;
}
